package com.ruoyi.purchase.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.product.domain.SysProduct;
import com.ruoyi.product.mapper.SysProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.purchase.mapper.SysPurchaseMapper;
import com.ruoyi.purchase.domain.SysPurchase;
import com.ruoyi.purchase.service.ISysPurchaseService;

/**
 * 商品进货Service业务层处理
 * 
 * @author chenjiujia
 * @date 2025-04-29
 */
@Service
public class SysPurchaseServiceImpl implements ISysPurchaseService 
{
    @Autowired
    private SysPurchaseMapper sysPurchaseMapper;

    @Autowired
    private SysProductMapper sysProductMapper;


    /**
     * 查询商品进货
     * 
     * @param id 商品进货主键
     * @return 商品进货
     */
    @Override
    public SysPurchase selectSysPurchaseById(Long id)
    {
        return sysPurchaseMapper.selectSysPurchaseById(id);
    }

    /**
     * 查询商品进货列表
     * 
     * @param sysPurchase 商品进货
     * @return 商品进货
     */
    @Override
    public List<SysPurchase> selectSysPurchaseList(SysPurchase sysPurchase)
    {
        return sysPurchaseMapper.selectSysPurchaseList(sysPurchase);
    }

    /**
     * 新增商品进货
     * 
     * @param sysPurchase 商品进货
     * @return 结果
     */
    @Override
    public int insertSysPurchase(SysPurchase sysPurchase)
    {

        // 1.预处理：自动计算总金额
        BigDecimal quantity = new BigDecimal(sysPurchase.getPurchaseQuantity());
        BigDecimal price = sysPurchase.getPurchasePrice();
        BigDecimal totalAmount = price.multiply(quantity);
        // 2.预处理：设置总金额
        sysPurchase.setTotalAmount(totalAmount);
        sysPurchase.setCreateTime(DateUtils.getNowDate());
        // 3.预处理：根据supplier_id查询product_id
        SysProduct product = new SysProduct();
        product.setSupplierId(sysPurchase.getSupplierId());
        System.out.println("----------------:"+sysPurchase.getSupplierId());
        // 3.1查询出当前供应商对应的product商品信息
        List<SysProduct> sysProducts = sysProductMapper.selectSysProductList(product);
        SysProduct sysProduct = sysProducts.get(0);
        System.out.println(sysProduct);
        // 3.2查询出查询出当前供应商对应的product_id
        Long product_id = sysProduct.getId();
//        String product_name = sysProduct.getProductName();
        // 4. 补全进货表product_id
        sysPurchase.setProductId(product_id);
        // 5. 插入进货表
        int rows = sysPurchaseMapper.insertSysPurchase(sysPurchase);
        // 6. 更新商品表?哪一张？
        SysProduct sysProductNew = new SysProduct();
        // 根据商品id更新
        sysProductNew.setId(sysPurchase.getProductId());
        // 更新库存：
        sysProductNew.setQuantity(sysProduct.getQuantity()+sysPurchase.getPurchaseQuantity());
        sysProductMapper.updateSysProduct(sysProductNew);
        return rows;
    }

    /**
     * 修改商品进货
     * 
     * @param sysPurchase 商品进货
     * @return 结果
     */
    @Override
    public int updateSysPurchase(SysPurchase sysPurchase)
    {
        // 获取修改前商品库存数据
        Long sysPurchaseId = sysPurchase.getId();
        SysPurchase sysPurchaseOld = sysPurchaseMapper.selectSysPurchaseById(sysPurchaseId);
//        SysProduct sysProduct = new SysProduct();
//        // 1. 自动计算总金额
//        BigDecimal quantity = new BigDecimal(sysPurchase.getPurchaseQuantity());
//        BigDecimal price = sysPurchase.getPurchasePrice();
//        BigDecimal totalAmount = price.multiply(quantity);
//        // 2. 设置总金额
//        sysPurchase.setTotalAmount(totalAmount);
//        sysPurchase.setCreateTime(DateUtils.getNowDate());
//        // 3. 插入进货表
//        int rows = sysPurchaseMapper.updateSysPurchase(sysPurchase);
//
//        Long result = sysPurchase.getPurchaseQuantity() - sysPurchaseOld.getPurchaseQuantity();
//        // 如果修改后的进货量大于原本的，
////        if (result < 0){
////            // 4.更新商品表
////            if (sysProduct.getQuantity()+result < 0){
////                throw new RuntimeException("更新失败，更新的数量");
////            }
////            sysProduct.setId(sysPurchase.getProductId());
////            sysProduct.setQuantity(sysProduct.getQuantity()+result);
////            sysProductMapper.updateSysProduct(sysProduct);
////        }
//            sysProduct.setId(sysPurchase.getProductId());
//            sysProduct.setQuantity(sysProduct.getQuantity()+result);
//            sysProductMapper.updateSysProduct(sysProduct);
        // 1.预处理：自动计算总金额
        BigDecimal quantity = new BigDecimal(sysPurchase.getPurchaseQuantity());
        BigDecimal price = sysPurchase.getPurchasePrice();
        BigDecimal totalAmount = price.multiply(quantity);
        // 2.预处理：设置总金额
        sysPurchase.setTotalAmount(totalAmount);
        sysPurchase.setCreateTime(DateUtils.getNowDate());
        // 3.预处理：根据supplier_id查询product_id
        SysProduct product = new SysProduct();
        product.setSupplierId(sysPurchase.getSupplierId());
        // 3.1查询出当前供应商对应的product商品信息
        List<SysProduct> sysProducts = sysProductMapper.selectSysProductList(product);
        SysProduct sysProduct = sysProducts.get(0);
        // 3.2查询出查询出当前供应商对应的product_id
        Long product_id = sysProducts.get(0).getId();
        // 4. 补全进货表product_id
        sysPurchase.setProductId(product_id);
        // 5. 插入进货表
        int rows = sysPurchaseMapper.updateSysPurchase(sysPurchase);
        Long result = sysPurchase.getPurchaseQuantity() - sysPurchaseOld.getPurchaseQuantity();
        // 6. 更新商品表?哪一张？
        SysProduct sysProductNew = new SysProduct();
        // 根据商品id更新
        sysProductNew.setId(sysPurchase.getProductId());
        // 更新库存：
        sysProductNew.setQuantity(sysProduct.getQuantity()+result);
        sysProductMapper.updateSysProduct(sysProductNew);
        return rows;
    }

    /**
     * 批量删除商品进货
     * 
     * @param ids 需要删除的商品进货主键
     * @return 结果
     */
    @Override
    public int deleteSysPurchaseByIds(Long[] ids)
    {
        return sysPurchaseMapper.deleteSysPurchaseByIds(ids);
    }

    /**
     * 删除商品进货信息
     * 
     * @param id 商品进货主键
     * @return 结果
     */
    @Override
    public int deleteSysPurchaseById(Long id)
    {
        return sysPurchaseMapper.deleteSysPurchaseById(id);
    }
}
